package com.mb.app.controller;

import com.mb.app.domain.TbProductPic;
import com.mb.app.service.ITbProductPicService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品图片  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-02
 */
@Controller
@RequestMapping("/app/productPic")
public class TbProductPicController extends BaseController {


    @Autowired
    private ITbProductPicService productPicService;

    /**
     *
     * @param model
     * @param productId
     * @return
     */
    @GetMapping("/index/{productId}")
    public String childIndex(Model model, @PathVariable("productId") String productId){
        model.addAttribute("productId",productId);
        return "/app/productPic/productPic_list";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbProductPic> list = productPicService.queryList(query);
        int total = productPicService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }


    @GetMapping("/dtl/{id}")
    String dtl(@PathVariable("id") Long id, Model model){
        model.addAttribute("productPicInfo",productPicService.getDtl(id));
        return "/app/productPic/productPic_info";
    }


    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Long id, Model model){
        model.addAttribute("productPicInfo",productPicService.getDtl(id));
        return "/app/productPic/productPic_edit";
    }

    @ResponseBody
    @PostMapping("/update")
    public R update(TbProductPic productPic){
        productPic.setUpdateUserId(getUserId());
        int result = productPicService.update(productPic);
        return R.ok();
    }

    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("Id") Long Id){
        Long delUserId = getUserId();
        int result = productPicService.remove(Id,delUserId);
        return R.ok();
    }

}
